﻿using System;
using System.Data;
using System.Drawing;
using System.Web.UI.WebControls;
using HiauPdm.Bll;
using HiauPdm.Web;

namespace HiauPdm.Web
{
    public partial class WzCkMx : PageBase
    {
        protected override bool IsAccess()
        {
            return IsAccess("Jsc/WzCk.aspx");
        }

        /// <summary>
        /// 加载
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack) { return; }
            BtnSave.OnClientClick = "if(CheckSave()){this.disabled=true;" + Page.ClientScript.GetPostBackEventReference(BtnSave, null) + ";}return false;";
            BindData(int.Parse(Request.QueryString["id"]), Request.QueryString["cz"]);
            Details1.Focus();
        }

        /// <summary>
        /// 绑定刷新数据
        /// </summary>
        private void BindData(int id, string sCz)
        {
            // 查询数据
            DataSet ds = BCkGl.GetWzCkById(id);
            Details1.DataSource = ds;
            Details1.DataBind();
            // 设置操作权限
            if (sCz == "ck" || ds == null)
            {
                for (int i = 0; i < Details1.Rows.Count; i++)
                {
                    ((TextBox)Details1.Rows[i].Cells[1].Controls[1]).ReadOnly = true;
                    ((TextBox)Details1.Rows[i].Cells[1].Controls[1]).BackColor = Color.FromArgb(0xE7F3FF);
                }
                LblTitle.Text = "查看";
                BtnSave.Visible = false;
            }
            else if (sCz == "bj")
            {
                if ((decimal)ds.Tables[0].Rows[0]["原始数量"] < 0m)
                {
                    // 负数不准修改实发数量
                    ((TextBox)Details1.Rows[8].Cells[1].Controls[1]).ReadOnly = true;
                    ((TextBox)Details1.Rows[8].Cells[1].Controls[1]).BackColor = Color.FromArgb(0xE7F3FF);
                }
                LblTitle.Text = "编辑";
                BtnSave.Text = BtnSave.ToolTip = "保存";
                BtnSave.Visible = true;
            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        protected void BtnSave_Click(object sender, EventArgs e)
        {
            if (!ShowErr(BCkGl.SaveWzCk((int)Details1.DataKey.Value,
                Convert.ToDecimal(((TextBox)Details1.Rows[7].Cells[1].Controls[1]).Text.Trim()),
                Convert.ToDecimal(((TextBox)Details1.Rows[8].Cells[1].Controls[1]).Text.Trim()),
                ((TextBox)Details1.Rows[10].Cells[1].Controls[1]).Text)))
            {
                Response.Write("<script type='text/javascript'>top.returnValue=true;top.close();</script>");
                Response.End();
            }
        }
    }
}